/**
 * Copyright © 2014 Winit Corp. All rights reserved.
 * This software is proprietary to and embodies the confidential
 * technology of Winit Corp.  Possession, use, or copying
 * of this software and media is authorized only pursuant to a
 * valid written license from Winit Corp or an authorized sublicensor.
 */
package com.winit.openapi.erp.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.erp.spi.product.ProductService;
import com.winit.erp.spi.product.TaxRuleService;
import com.winit.erp.spi.product.command.QueryProductCommand;
import com.winit.erp.spi.product.command.QueryTaxRuleCommand;
import com.winit.erp.spi.product.vo.Product;
import com.winit.erp.spi.product.vo.TaxRule;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;

/**
 * TODO weifeng.liu: Change to the actual description of this class
 * 
 * @version Revision History
 * 
 * <pre>
 * Author     Version       Date        Changes
 * weifeng.liu    1.0           2015年3月17日     Created
 * 
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/erp/product")
public class ProductController extends BaseController {

    @Resource
    private TaxRuleService taxRuleService;
    @Resource
    private ProductService erpProductService;

    @RequestMapping(value = "/queryTaxRule")
    public String queryTaxRule(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();

        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            QueryTaxRuleCommand command = new QueryTaxRuleCommand();
            command.setHsCode(json.getString("hsCode"));
            command.setCountryCode(json.getString("countryCode"));
            TaxRule rule = taxRuleService.queryTaxRuleByKey(command);
            responseMsg.setData(rule);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }

    @RequestMapping(value = "/queryProductByCode")
    public String queryProductByCode(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();

        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            QueryProductCommand command = new QueryProductCommand();
            command.setCode(json.getString("code"));
            command.setBpartnerId(json.getLong("bpartnerId"));
            List<Product> list = erpProductService.queryProductByCode(command);
            responseMsg.setData(list);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }

}
